package com.resp;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

@WebServlet("/Servlet12")
public class Servlet12 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("执行了servlet12....");

        //获取ServletContext对象
        ServletContext servletContext = req.getServletContext();

        //获取资源的真实路径
        //String realPath = servletContext.getRealPath("a4.properties");//jerry
        //String realPath = servletContext.getRealPath("a4.properties");//jerry
        //String realPath = servletContext.getRealPath("aaa/a3.properties");//jack
        String realPath = servletContext.getRealPath("/WEB-INF/classes/a2.properties");//jack

        //创建集合对象
        Properties properties = new Properties();

        //加载配置文件
        properties.load(new FileInputStream(realPath));

        String name = properties.getProperty("name");

        System.out.println(name);

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
